<?php

/**
 * User: day
 * Date: 2018/09/11
 * Time: 19:05
 * Description:
 * AG 视讯结果，暂时不接入，每一个游戏都要处理
 */

namespace App\Console\Commands\ThirdOrder;


use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\AGAPIService;
use App\Models\Order;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Log;


class AGVideoResult {
    /**
     * 程序入口
     */
    public function handle($argv = null)
    {
        $this->info("AG视讯结果抓取 开始处理:");
        try {
            $this->solve();
        } catch(\Throwable $e) {
            $msg = "请修复程序再运行:\nMessage:" . $e->getMessage() . "\nFile:" . $e->getFile() . "\nLine:" . $e->getLine() . "\ntrace: " . $e->getTraceAsString() . PHP_EOL;
            $this->info($msg);
            $this->error($msg);
        }
        $this->info("AG视讯结果抓取 处理完成");
    }

    /**
     * 处理程序
     */
    public function solve()
    {
        $agService = new AGAPIService();
        $agService->init();
        $datas = Order::find()
            ->where(['type_name' => 'AG', 'result' => '', 'order_type' => 2])
            ->orderBy('send_date')
            ->all();
        $results = [];
        foreach ($datas as $key => $model) {
            $result = $results[$model->game_code];
            if (empty($result)) {
                $startTime = strtotime($model->date);
                $endTime = $startTime + 60*60*12;
                $result = $agService->getVideoGameResult($model->game_code, $startTime, $endTime);

                // gmcode:局號
                // begintime:開始時間
                // closetime:結束時間
                // dealer:荷官名稱
                // gametype:游戲類型
                // shoecode:靴號
                // bankerpoint: 莊分數，附錄莊分數結果說明
                // playerpoint: 閒分數，附錄閑分數結果說明 carnum:牌的張數,可用于判斷百家樂的大小玩法
                // pair:對子結果(0 沒有對子,1 莊對,2 閑對,3 莊對閑對)
                // dragonpoint:龍點數
                // tilgerpoint:虎點數
                // cardlist: 牌結果描述,格式說明請參考，附錄發牌資料cardlist結果說明
                // vid:視頻 id
                // platformType:平臺類型為 AGIN

            // 'gameCode' => 'GB004189060JU',
            // 'begintime' => '2018-09-06 09:19:20',
            // 'closetime' => '2018-09-06 09:19:57',
            // 'dealer' => 'Lux',
            // 'shoecode' => '23575',
            // 'flag' => '1',
            // 'bankerpoint' => '9',
            // 'playerpoint' => '0',
            // 'cardnum' => '6',
            // 'pair' => '0',
            // 'dragonpoint' => '0',
            // 'tigerpoint' => '0',
            // 'cardlist' => '19 59 7;41 17 9',
            // 'vid' => 'B004',
            // 'platformtype' => 'AGIN',
            // 'gametype' => 'BAC',

                // flag:結果狀態,0 為無效 ,1 為有效
                $results[$model->game_code] = $result;
            }
            Log::info($result);exit;
            $model->result = json_encode($result);
            $resultStr = "";
            switch ($result['gametype']) {
                case "DT":
                    $resultStr = sprintf("龙(%s)虎(%s)", $result['dragonpoint'], $result['tigerpoint']);
                    break;
                default:
                    $resultStr = sprintf("庄(%s)闲(%s)", $result['bankerpoint'], $result['playerpoint']);
            }
            $model->bet_content .= " 开奖结果：" . $resultStr;
            $res = $model->save();
            Log::info("Update Video Resutl: " . $model->order_number . ", result:" . $model->bet_content . ", error: " . json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
        }
    }
}